package com.ruoyi.web.controller.video;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.service.DictService;
import com.ruoyi.video.domain.TeacherInfo;
import com.ruoyi.video.service.TeacherInfoService;
import com.ruoyi.web.core.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 讲师 信息操作处理
 *
 * @author pig13
 * @date 2018-11-11
 */
@Controller
@RequestMapping("/video/teacherInfo")
public class TeacherInfoController extends BaseController {
    private String prefix = "video/teacherInfo";

    @Autowired
    private TeacherInfoService teacherInfoService;
    @Autowired
    private DictService dictService;
    private List<TeacherInfo> list;

    @RequiresPermissions("video:teacherInfo:view")
    @GetMapping()
    public String teacherInfo() {
        return prefix + "/teacherInfo";
    }

    /**
     * 查询讲师列表
     */
    @RequiresPermissions("video:teacherInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TeacherInfo teacherInfo) {
        startPage();
        List<TeacherInfo> list = teacherInfoService.selectTeacherInfoList(teacherInfo);
        return getDataTable(list);
    }

    /**
     * 新增讲师
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存讲师
     */
    @RequiresPermissions("video:teacherInfo:add")
    @Log(title = "讲师", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TeacherInfo teacherInfo) {
        teacherInfo.setCreateBy(ShiroUtils.getLoginName());
        teacherInfo.setCreateTime(new Date());
        String status = teacherInfo.getStatus();
        if ("on".equalsIgnoreCase(status)) {
            teacherInfo.setStatus("0");
        } else if ("off".equalsIgnoreCase(status) || "".equalsIgnoreCase(status) || null == status) {
            teacherInfo.setStatus("1");
        }
        return toAjax(teacherInfoService.insertTeacherInfo(teacherInfo));
    }

    /**
     * 修改讲师
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        TeacherInfo teacherInfo = teacherInfoService.selectTeacherInfoById(id);
        mmap.put("teacherInfo", teacherInfo);
        return prefix + "/edit";
    }

    /**
     * 查看讲师
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap mmap) {
        TeacherInfo teacherInfo = teacherInfoService.selectTeacherInfoById(id);
        mmap.put("teacherInfo", teacherInfo);
        return prefix + "/detail";
    }

    /**
     * 修改保存讲师
     */
    @RequiresPermissions("video:teacherInfo:edit")
    @Log(title = "讲师", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TeacherInfo teacherInfo) {
        String status = teacherInfo.getStatus();
        if ("on".equalsIgnoreCase(status)) {
            teacherInfo.setStatus("0");
        } else if ("off".equalsIgnoreCase(status) || "".equalsIgnoreCase(status) || null == status) {
            teacherInfo.setStatus("1");
        }
        return toAjax(teacherInfoService.updateTeacherInfo(teacherInfo));
    }

    /**
     * 删除讲师
     */
    @RequiresPermissions("video:teacherInfo:remove")
    @Log(title = "讲师", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(teacherInfoService.deleteTeacherInfoByIds(ids));
    }

}
